package com.zongzi.stock.utils;

import java.util.Date;

/**
 * Created by Dell on 2017/5/18.
 */

public class TimeUtils {
    /**
     * 判断是否是股票交易时间
     * 9:15-11:30
     * 13:00-15:00
     * 提早ERROR_TIME分钟开始，晚ERROR_TIME分钟结束
     * 返回-1：9:15-11:30, 13:00-15:00, 中国股市交易时间
     * 返回0: 15:00之后，不再更新数据
     * 返回延时时间Min:9:15之前，或者11:30-13:00
     * @return
     */
    private static final int ERROR_TIME = 1;
    private static final int MORNING_START = 9 * 60 + 15;
    private static final int MORNING_END = 11 * 60 + 30;
    private static final int AFTERNOON_START = 13 * 60 + 0;
    private static final int AFTERNOON_END = 15 * 60 + 0;

    public static int delayStockTime(){
        int delay = 0;
        Date now = new Date();
        int hour = now.getHours();
        int min = now.getMinutes();
        int allMin = hour * 60 + min;
        if (allMin > (MORNING_START - ERROR_TIME) && allMin < (MORNING_END + ERROR_TIME)) {
            delay = -1;
        } else if (allMin > (AFTERNOON_START - ERROR_TIME) && allMin < (AFTERNOON_END + ERROR_TIME)) {
            delay = -1;
        }else if(allMin > (AFTERNOON_END + ERROR_TIME)){
            delay = 0;
        }else if (allMin < (MORNING_START - ERROR_TIME)) {
        	delay = MORNING_START - ERROR_TIME - allMin;
        }else if(allMin > (MORNING_END + ERROR_TIME) && allMin < (AFTERNOON_START - ERROR_TIME)){
            delay = AFTERNOON_START - ERROR_TIME - allMin;
        }
        return delay;
    }

    public static String getTimeString(long time){
        int transTime = (int) (time/100000);
        int min = transTime%100;
        int hour = transTime/100;
        String minString;
        String hourString;
        if (min == 0) {
            minString = "00";
        }else if (min < 10) {
            minString = "0" + min;
        }else{
            minString = min + "";
        }
        if (hour == 0) {
            hourString = "00";
        }else if (hour < 10) {
            hourString = "0" + hour;
        }else{
            hourString = hour + "";
        }
        return hourString + ":" + minString;
    }
}
